<?php

namespace api\modules\v1\forms\base;

use common\models\base\Job;
use Yii;
use yii\base\Model;


class JobForm extends Model
{
    /**
     * @var int $id ID
     * @var int $job_name 岗位名称
     * @var string $sort 排序
     * @var int $status 状态
     */
    public $id;
    public $job_name;
    public $sort;
    public $status;

    /**
     * @var Job $_job
     */
    protected $_job;


    public function rules()
    {
        return [
            // 参数验证
            [['job_name', 'sort', 'status'], 'required', 'message' => '{attribute}不能为空', 'on' => ['create']],
            [['id', 'job_name', 'sort', 'status'], 'required', 'message' => '{attribute}不能为空', 'on' => ['edit']],
            [['id'], 'required', 'message' => '{attribute}不能为空', 'on' => ['delete']],

            [['id'], 'checkId', 'skipOnEmpty' => false, 'on' => ['edit', 'delete']],
            [['job_name'], 'checkJobName', 'skipOnEmpty' => false, 'on' => ['create', 'edit']],
            [['sort'], 'checkSort', 'skipOnEmpty' => true, 'on' => ['create', 'edit']],
            [['status'], 'checkStatus', 'skipOnEmpty' => false, 'on' => ['create', 'edit']],
        ];
    }

    public function scenarios()
    {
        $scenarios = parent::scenarios();

        // 新增
        $scenarios['create'] = ['job_name', 'sort', 'status'];

        // 编辑
        $scenarios['edit'] = ['id', 'job_name', 'sort', 'status'];

        // 删除
        $scenarios['delete'] = ['id'];

        return $scenarios;
    }

    /**
     * @return array
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'job_name' => '岗位名称',
            'sort' => '排序',
            'status' => '状态',
        ];
    }
    /**
     * 检验岗位ID
     * @param $attribute
     * @return void
     */
    public function checkId($attribute): void
    {
        if ($this->hasErrors()) {
            return;
        }

        $job = $this->getJob();
        if (!$job) {
            $this->addError($attribute, '岗位不存在');
            return;
        }

        if ($job->status == Job::STATUS_DELETE) {
            $this->addError($attribute, '岗位已删除');
            return;
        }
    }

    /**
     * 校验岗位名称
     * @param $attribute
     * @return void
     */
    public function checkJobName($attribute): void
    {
        if ($this->hasErrors()) {
            return;
        }

        $len = mb_strlen($this->job_name);
        if ($len > 20) {
            $this->addError($attribute, '岗位名称过长，最多20个字');
            return;
        }

        // 是否检验
        $isCheck = true;
        if ($this->getScenario() == 'edit') {
            $job = $this->getJob();
            // 岗位参数未改变【跳过】
            if ($job->job_name == $this->job_name) {
                $isCheck = false;
            }
        }

        if ($isCheck) {
            $one = Job::findOne([
                'job_name' => $this->job_name,
                'status' => [Job::STATUS_INVALID, Job::STATUS_ENABLE],
            ]);
            if ($one) {
                $this->addError($attribute, "岗位【{$this->job_name}】已存在");
                return;
            }
        }
    }

    /**
     * 校验岗位排序
     * @param $attribute
     * @return void
     */
    public function checkSort($attribute): void
    {
        if ($this->hasErrors()) {
            return;
        }

        if ($this->sort < 0) {
            $this->addError($attribute, '排序值不能小于0');
            return;
        }
    }

    /**
     * 校验岗位状态
     * @param $attribute
     * @return void
     */
    public function checkStatus($attribute): void
    {
        if ($this->hasErrors()) {
            return;
        }

        if (!in_array($this->status, [Job::STATUS_INVALID, Job::STATUS_ENABLE])) {
            $this->addError($attribute, "非法状态");
            return;
        }
    }

    /**
     * 获取岗位信息
     * @return Job|null
     */
    public function getJob(): ?Job
    {
        if (empty($this->_job)) {
            $this->_job = Job::findOne($this->id);
        }

        return $this->_job;
    }
}
